package com.xb.loan.encryptor.handler.impl;


import com.xb.loan.encryptor.LotteryException;
import com.xb.loan.encryptor.bean.LotteryBean;
import com.xb.loan.encryptor.bean.request.other.AddPartnerPubKeyReq;
import com.xb.loan.encryptor.bean.response.other.AddPartnerPubKeyRep;
import com.xb.loan.encryptor.handler.BussinessHandler;
import com.xb.loan.encryptor.util.Const;
import com.xb.loan.encryptor.util.ToolUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;

/**
 * 添加合作商公钥
 * 
 * @author shiyin.chen
 * 
 */
public class AddPartnerPubKeyHandler implements BussinessHandler {
	
	private static Log log = LogFactory.getLog(AddPartnerPubKeyHandler.class);

	@Override
	public LotteryBean doBussiness(LotteryBean lotteryBean)
			throws LotteryException {

		AddPartnerPubKeyReq req = (AddPartnerPubKeyReq) lotteryBean;
		
		String keyPath = Const.KEY_PATH + File.separatorChar + "partner" + File.separatorChar + req.getPartnerId() + File.separatorChar + "partnerPubkey.txt";
		
		log.info("AddPartnerPubKeyHandler start add partner " + req.getPartnerId() + " pub key :" + req.getPubKey() + ",path:" + keyPath);
		
		ToolUtil.wirteIsAppend(keyPath, req.getPubKey(), false);
		
		AddPartnerPubKeyRep rep = new AddPartnerPubKeyRep();
		
		rep.setHead(LotteryBean.createHead("706", req.getHead().getGuid()));
		rep.setPartnerId(req.getPartnerId());
		
		return rep;
	}

}
